package com.prp.gwt.client.user.article;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.prp.gwt.client.ClientUtility;
import com.prp.gwt.client.Host;
import com.prp.gwt.client.vo.CommentList;
import com.prp.gwt.client.vo.Entity;
import com.prp.gwt.client.vo.EntityList;
import com.prp.gwt.client.vo.SubjectCategoryList;
import com.prp.gwt.client.vo.SubjectChildCategoryList;
import com.prp.gwt.client.vo.SubjectRecommend;
import com.prp.gwt.client.vo.SubjectRecommendList;
import com.prp.gwt.client.widgets.MessageDialogBox;
import com.prp.gwt.client.widgets.user.article.ArticleEditPanel;
import com.prp.gwt.client.widgets.user.article.CreateEntityPanel;
import com.prp.gwt.client.widgets.user.article.EntityRecommendPanel;
import com.prp.gwt.client.widgets.user.article.RecommendPanel;
import com.prp.gwt.client.widgets.user.article.RecommendSummaryItem;
import com.prp.gwt.client.widgets.user.article.SearchResultPanel;
import com.prp.gwt.client.widgets.user.article.SideBar;
import com.prp.gwt.client.widgets.user.article.SideBarCategory;
import com.prp.gwt.client.widgets.user.article.SystemTopEntityPanel;
import com.prp.gwt.client.widgets.user.article.SystemTopRecommendPanel;

public class ArticleListener {

	private static ArticleListener instance;
	private ArticleServiceAsync service = GWT.create(ArticleService.class);

	public static ArticleListener getInstance() {
		if (instance == null)
			instance = new ArticleListener();
		return instance;
	}

	public void search(final String keyword) {
		service.search(keyword, new AsyncCallback<EntityList>() {
			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}

			@Override
			public void onSuccess(EntityList result) {
				SearchResultPanel.getInstance().load(keyword + "的搜索结果", result);
			}
		});
	}

	public void createEntity(int childCategoryId, String entityName) {
		service.createEntity(childCategoryId, entityName,
				new AsyncCallback<Entity>() {
					@Override
					public void onFailure(Throwable caught) {
						ClientUtility.handleException(caught);
					}

					@Override
					public void onSuccess(Entity result) {
						if (result == null)
							MessageDialogBox.show("创建条目失败", "内容过期，请刷新后再试");
						else {
							// Window.alert("创建成功");
							History.newItem(ArticleHistoryHandler.PREFIX_ENTITY
									+ result.getId());
						}
					}
				});
	}

	public void getSubjectCategoryList() {
		service
				.getSubjectCategoryList(new AsyncCallback<SubjectCategoryList>() {
					@Override
					public void onFailure(Throwable caught) {
						ClientUtility.handleException(caught);
					}

					@Override
					public void onSuccess(SubjectCategoryList result) {
						SideBar.getInstance().load(result);
					}
				});

	}

	public void getSubjectChildCategoryList(int categoryId,
			final SideBarCategory widget) {
		service.getSubjectChildCategoryList(categoryId,
				new AsyncCallback<SubjectChildCategoryList>() {
					@Override
					public void onFailure(Throwable caught) {
						ClientUtility.handleException(caught);
					}

					@Override
					public void onSuccess(SubjectChildCategoryList result) {
						if (result == null || result.isEmpty())
							Window.Location.replace(Host.getPAGE_NOT_FOUND());
						if (widget != null)
							widget.load(result);
						else
							CreateEntityPanel.getInstance().load(result);

					}
				});
	}

	public void getSubjectRecommendList(int entityId) {
		service.getSubjectRecommendList(entityId,
				new AsyncCallback<SubjectRecommendList>() {
					@Override
					public void onFailure(Throwable caught) {
						ClientUtility.handleException(caught);
					}

					@Override
					public void onSuccess(SubjectRecommendList result) {
						EntityRecommendPanel.getInstance()
								.loadRecommend(result);
					}
				});
	}

	public void getEntity(int entityId) {
		service.getEntity(entityId, new AsyncCallback<Entity>() {
			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}

			@Override
			public void onSuccess(Entity result) {
				if (result == null)
					Window.Location.replace(Host.getPAGE_NOT_FOUND());
				else {
					EntityRecommendPanel.getInstance().loadEntity(result);
					ArticleEditPanel.getInstance().load(result);
				}
			}
		});
	}

	public void getSubjectRecommend(int id) {
		service.getSubjectRecommend(id, new AsyncCallback<SubjectRecommend>() {
			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}

			@Override
			public void onSuccess(SubjectRecommend result) {
				if (result == null)
					Window.Location.replace(Host.getPAGE_NOT_FOUND());
				else
					RecommendPanel.getInstance().loadRecommend(result);
			}
		});
	}

	public void getCommentList(int subjectId) {
		service.getCommentList(subjectId, new AsyncCallback<CommentList>() {
			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}

			@Override
			public void onSuccess(CommentList result) {
				RecommendPanel.getInstance().loadCommentList(result);
			}
		});
	}

	public void addComment(final int subjectId, String content, int score) {
		service.addComment(subjectId, content, score,
				new AsyncCallback<Void>() {
					@Override
					public void onFailure(Throwable caught) {
						ClientUtility.handleException(caught);
					}

					@Override
					public void onSuccess(Void result) {
						getCommentList(subjectId);
					}
				});
	}

	public void getEntityList(final String childCategoryName,
			int childCategoryId) {
		service.getEntityList(childCategoryId, new AsyncCallback<EntityList>() {
			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}

			@Override
			public void onSuccess(EntityList result) {
				SearchResultPanel.getInstance().load(
						"实体" + childCategoryName + "列表", result);
			}
		});
	}

	public void getRecentEntityList() {
		service.getRecentEntity(new AsyncCallback<EntityList>() {
			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}

			@Override
			public void onSuccess(EntityList result) {
				SearchResultPanel.getInstance().load("最近创建实体", result);
			}
		});
	}

	public void createRecommend(int entityId, String title, String content) {
		service.createRecommend(entityId, title, content,
				new AsyncCallback<Integer>() {
					@Override
					public void onFailure(Throwable caught) {
						ClientUtility.handleException(caught);
					}

					@Override
					public void onSuccess(Integer result) {
						if (result == -1)
							Window.alert("添加推荐失败");
						else
							History
									.newItem(ArticleHistoryHandler.PREFIX_RECOMMEND
											+ result);
					}
				});
	}

	public void getTopRecommend() {
		service.getTopRecommend(new AsyncCallback<SubjectRecommendList>() {
			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}

			@Override
			public void onSuccess(SubjectRecommendList result) {
				SystemTopRecommendPanel.getInstance().loadRecommendationList(
						result);
			}
		});
	}

	public void getTopEntity() {
		service.getTopEntity(new AsyncCallback<EntityList>() {
			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}

			@Override
			public void onSuccess(EntityList result) {
				SystemTopEntityPanel.getInstance().loadEntityList(result);
			}
		});
	}

	public void addOneClick(int subjectId){
		service.addOneClick(subjectId, new AsyncCallback<Void>(){
			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}
			@Override
			public void onSuccess(Void result) {
				
			}
		});
	}
	
	public void deleteRecommendation(int subjectId, final RecommendSummaryItem item){
		service.deleteSubjectRecommendation(subjectId, new AsyncCallback<Void>(){
			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}
			@Override
			public void onSuccess(Void result) {
				item.removeFromParent();
			}
		});
	}
	
}
